package com.ag.lzw.service.impl;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;

import com.ag.contactTree.TreeNode;
import com.ag.contactTree.service.LocalTreeService;
import com.ag.lzw.service.GetDeptInTask;
import com.ag.lzw.vo.DeptInfo;
import com.ag.lzw.vo.DeptPeople;
import com.ag.zhh.vo.UserDept;
import com.ag.zhh.vo.UserDeptContact;

/**
 * @author zhiweiofli
 * @version1.0 GetDeptInTask接口的实现类 本类所获取的数据基本依赖于系统的通讯录数据树
 * */
@Service("getDeptInTaskImpl")
public class GetDeptInTaskImpl implements GetDeptInTask {

	private static Logger logger = Logger.getLogger(GetDeptInTaskImpl.class);

	@Resource(name = "LocalTreeService")
	private LocalTreeService lts;

	/**
	 * 根据类型返回顶级部门（部门树有三棵）
	 * 
	 * @return List<UserDept>
	 */
	@Override
	public UserDept getTopDept(String type) {

		List<UserDept> list = new ArrayList<UserDept>();

		switch (Integer.parseInt(type)) {

		case 1:
			TreeNode tn1 = lts.getMedicineTreeRoot();
			//TreeNode tn1 = lts.getCoordinatTreeRoot();
			return (UserDept) tn1.getBindData();

		case 2:
			TreeNode tn2 = lts.getEmergencyTreeRoot();
			return (UserDept) tn2.getBindData();

		case 3:
			TreeNode tn3 = lts.getExternalTreeRoot();
			return (UserDept) tn3.getBindData();

		default:
			return null;

		}
	}

	/**
	 * 返回所有一级部门的信息
	 * 
	 * @return deptInfos List<DeptInfo>
	 * */
	@Override
	public List<DeptInfo> getSuperiorDepts(String type) {
		List<DeptInfo> deptInfos = new ArrayList<DeptInfo>();

		UserDept ud = this.getTopDept(type);
		if (ud != null) {
			List<TreeNode> tns = lts.getChildrenByFDId(ud.getUdId()); // 查询其子部门
			for (TreeNode tn : tns) { // 遍历子部门的节点
				DeptInfo deptInfo = new DeptInfo();
				UserDept sud = (UserDept) tn.getBindData();

				deptInfo.setId(sud.getUdId());
				deptInfo.setName(sud.getUdName());
				deptInfo.setFid(ud.getUdId());

				if (sud.getTbUser() != null) {
					deptInfo.setUid(sud.getTbUser().getUId());
				} else {
					deptInfo = null;
					continue; // 如果该部门没有负责人，则跳过--没有负责人的部门不呢个接收任务的分派
				}
				deptInfos.add(deptInfo);
				/*
				 * if (sud.getTbUserDept() != null) {
				 * deptInfo.setFid(sud.getTbUserDept().getUdId()); } else {
				 * deptInfo.setFid(null); }
				 */
			}
			return deptInfos;
		} else {
			return null;
		}

	}

	/*
	 * @Override public List<TreeNode> getSubstation(){ List<TreeNode> subDepts
	 * = new ArrayList<TreeNode>();; TreeNode root = ts.getMedicineTreeRoot();
	 * //获取根节点的下一级子节点 List<TreeNode> subNodes =
	 * ts.getChildrenByFDId(root.getNodeId()); Iterator it =
	 * subNodes.iterator(); //遍历拥有子节点的节点 while(it.hasNext()){ TreeNode tn =
	 * (TreeNode)it.next(); List list = tn.getChildren(); if(list.size()>0){
	 * subDepts.add(tn); } } return subDepts; }
	 */
	/**
	 * 根据部门ID，从用户部门通信树中获取其属下的人员
	 * 
	 * @param D_ID
	 *            部门ID String
	 * @return 整个部门联系人的信息 Set<UserDeptContact>
	 * */
	@Override
	public List<DeptPeople> getUsersByDeptId(String D_ID) {
		Set<UserDeptContact> users = null;
		TreeNode tn = lts.getTreeNodeById(D_ID);
		if (tn != null) {
			users = new HashSet<UserDeptContact>(0);

			UserDept dept = (UserDept) tn.getBindData();
			users = dept.getTbUsers();

			List<DeptPeople> deptPeople = null;

			if (users.size() > 0) { // 用VO重新封装
				deptPeople = new ArrayList<DeptPeople>();
				Iterator it = users.iterator();
				while (it.hasNext()) {
					UserDeptContact udc = (UserDeptContact) it.next();
					DeptPeople up = new DeptPeople();
					up.setId(udc.getUId());
					up.setName(udc.getUName());
					up.setDeptID(D_ID);
					deptPeople.add(up);
				}
				return deptPeople;
			} else {
				return null;
			}
		} else {
			return null;
		}
	}

	/**
	 * 对getSubDeptsById方法返回数据进行重新封装
	 * 
	 * @param 部门ID
	 * @return 所有子部门的信息
	 * */
	@Override
	public List<DeptInfo> getSubDepts(String id) {
		List<DeptInfo> deptInfos = new ArrayList<DeptInfo>();
		DeptInfo deptInfo = null;
		Set<UserDept> depts = getSubDeptsById(id);
		if (depts != null) {
			Iterator<UserDept> it = depts.iterator();
			while (it.hasNext()) {
				UserDept ud = it.next();
				deptInfo = new DeptInfo();
				deptInfo.setId(ud.getUdId());
				deptInfo.setName(ud.getUdName());
				// 获取当前部门的负责人ID
				if (ud.getTbUser() != null) {
					deptInfo.setUid(ud.getTbUser().getUId());
				} else {
					deptInfo = null;
					continue;
				}
				// 获取当前部门的上级部门
				if (ud.getTbUserDept() != null) {
					deptInfo.setFid(ud.getTbUserDept().getUdId());
				} else {
					deptInfo.setFid(null);
				}

				deptInfos.add(deptInfo);
			}
		}

		return deptInfos;
	}

	/**
	 * 根据父部门的ID,从通信树中查找其子部门
	 * 
	 * @param D_ID
	 *            String 部门ID
	 * @return depts Set<UserDept> 子部门集合
	 * */
	@Override
	public Set<UserDept> getSubDeptsById(String D_ID) {

		TreeNode tn = lts.getTreeNodeById(D_ID);
		logger.info("---get sub dept by id:" + D_ID);
		if (tn != null) {
			UserDept ud = (UserDept) tn.getBindData();
			return ud.getTbUserDepts();
		} else {
			logger.info("Cannot found sud depts by id:" + D_ID);
			return null;
		}
	}

}
